package com.longxi.tianjintianbao.ui.presenter.main;

import android.content.Intent;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.greedao.controller.DataUserController;
import com.longxi.tianjintianbao.greedao.controller.DataWorkController;
import com.longxi.tianjintianbao.ui.activity.MyWidgetTestActivity;
import com.longxi.tianjintianbao.ui.activity.login.LoginActivity;
import com.longxi.tianjintianbao.ui.activity.main.AboutActivity;
import com.longxi.tianjintianbao.ui.activity.main.PersonalDataActivity;
import com.longxi.tianjintianbao.ui.activity.repair_publish.SelectCommunityActivity;
import com.longxi.tianjintianbao.ui.activity.work.WorkLayoutActivity;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.view.main.IMeFgView;
import com.longxi.tianjintianbao.utils.MyUtils;

import java.util.Arrays;
import java.util.List;

public class MeFgPresenter extends BasePresenter<IMeFgView> {

    public MeFgPresenter(BaseActivity context) {
        super(context);
    }

    public void meUi(){
        getView().getTvUsername().setText(MyApplication.loginUser.getUsername());
        getView().getCommunityName().setText(getBuildName());
        getView().getPostName().setText(MyApplication.loginUser.getDept());
    }

    public void loginOut() {
        DataUserController.updateAllByIsLogin();
//        MyApplication.IP="";
//        ApiRetrofit.clearInstance();
        MyApplication.loginUser = null;
        DataWorkController.replaceWork();
        MyUtils.showToast("退出成功");
        Intent intent = new Intent(mContext, LoginActivity.class);
        mContext.startActivity(intent);
        mContext.finish();
    }

    /**
     * 跳转到SelectCommunityActivity
     */
    public void toSelectCommunityActivity() {
        Intent intent = new Intent(mContext, SelectCommunityActivity.class);
        mContext.startActivityForResult(intent, AppConst.IntentRequstCode.ACTIVITY_SELECT_COMMUNITY);
    }

    /**
     * 跳转到SelectCommunityActivity
     */
    public void toPersonalDataActivity() {
        Intent intent = new Intent(mContext, PersonalDataActivity.class);
        mContext.startActivity(intent);
    }

    /**
     * 跳转到WorkLayoutActivity
     */
    public void toWorkLayoutActivity() {
        Intent intent = new Intent(mContext, WorkLayoutActivity.class);
        mContext.startActivity(intent);
    }

    /**
     * 跳转到AboutActivity
     */
    public void toAboutActivity() {
        Intent intent = new Intent(mContext, AboutActivity.class);
        mContext.startActivity(intent);
    }

    public void toMyWidgetTest() {
        Intent intent = new Intent(mContext, MyWidgetTestActivity.class);
        mContext.startActivity(intent);
    }

    /**
     * 获取用户选择的楼盘
     * @return
     */
    public String getBuildName(){
        String build = MyApplication.loginUser.build;
        String buildName = "";
        if("".equals(build)){
            return buildName;
        }
        List<String> buildList= Arrays.asList(build.replace(" ", "").split(","));

        switch (buildList.size()) {
            case 0:
                buildName = "您还未选择楼盘";
                break;
            case 1:
                buildName = buildList.get(0);
                break;
            case 2:
                buildName = buildList.get(0) + "等两个楼盘";
                break;
            default:
                buildName = buildList.get(0) + "等" + buildList.size() + "个楼盘";
                break;
        }
        return buildName;
    }
}
